<?php

   // Search any world in entire database

   set_time_limit(0);

   $database = 'awtestdb_dan';
   $username = 'awtestdbuser_dan';
   $password = 'awtestdbpwd_dan';
   $word = 'da/pictures/';
   $replace = 'pictures_da/';
   $enabler = 1;
  
   echo "Search for word: <b>".$word."</b><br>";
   echo "Replace with: <b>".$replace."</b><br><br>";
  
   $connect = mysql_connect('localhost',$username,$password);
   mysql_selectdb( $database, $connect );
  
   $j=0;
   $affected = 0;
   $tables = mysql_list_tables( $database );
  
   while( $line = mysql_fetch_row( $tables ) )
   {
       $fields = mysql_list_fields( $database, $line[0], $connect );
       $columns = mysql_num_fields( $fields );
      
       for ($i = 0; $i < $columns; $i++) {
               $nomField = mysql_field_name( $fields, $i );
			   
			
		   
			   
           $sql = "SELECT ".$nomField." FROM " . $line[0] .
               " WHERE " .
                   $nomField . " LIKE '%".$word."%'";
          
           $query = mysql_query( $sql );
		   
		   if ($enabler) {
			   
		   $sql = "update ".$line[0]." set " . $nomField .
               "=replace(" .$nomField .", '". $word . "', '" . $replace ."') WHERE " .
                   $nomField . " LIKE '%".$word."%'";
          
           $query2 = mysql_query( $sql ); 
		   
		   $affected += mysql_affected_rows ($connect);
		   }
           if( mysql_num_rows( $query ) > 0)
           {
              while ($result = mysql_fetch_array($query)) {
               	echo "Table: <b>".$line[0]."</b><br>";
               	echo "Field: <b>".$nomField."</b><br>";
               	echo "SQL: <b>".$sql."</b><br>";
				$startpos = htmlspecialchars (strpos($result[0], $word) - 100);
				$finalresult = htmlspecialchars (substr($result[0], $startpos, "200"));
               	//echo "Result: <b>" . str_replace( $word, "<font color='red'>". $word . "</font>", $result[0] ) . "</b>";
				echo "Updated: " . $finalresult . "<br>";
               	echo "<br><br>";
               	$j++;
			  }
           }
       }
   }
   echo "<br><br><br>";   
   echo "Results: ".$j . "<br>";
   echo "Affected rows: ".$affected;
  
?>
